package org.softmed.rest.editor.comps;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import org.restlet.Client;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.data.Response;
import org.restlet.resource.StringRepresentation;
import org.softmed.rest.editor.EditorUtil;
import org.softmed.rest.editor.commons.SwingUtil;
import org.softmed.rest.editor.commons.URIProvider;
import org.softmed.rest.editor.config.Parameter;
import org.softmed.rest.editor.xml.XStreamConfigurator;

import com.thoughtworks.xstream.XStream;

public abstract class ParameterEditor extends SimpleEditor implements
		ActionListener {
	protected Parameter parameter;

	protected CustomTextField name = new CustomTextField();
	protected CustomTextField value = new CustomTextField();
	protected JCheckBox active = new JCheckBox();
	protected JLabel id = new JLabel("id");

	public ParameterEditor(URIProvider uriProvider) {
		super(uriProvider);
		uri = "parameters";
		XStreamConfigurator.configureStreamerParameter(streamer);
		active.addActionListener(this);
		name.addActionListener(this);
		value.addActionListener(this);
//		setup();
	}

	// public ParameterEditor(URIProvider uriProvider) {
	// super(uriProvider);
	// uri = "parameters";
	// XStreamConfigurator.configureStreamerParameter(streamer);
	// this.uriProvider = uriProvider;
	// SwingUtil.setSize(this, EditorUtil.smallestWidth,
	// EditorUtil.smallestHeight);
	// MigLayout layout = new MigLayout("left, insets " +
	// EditorUtil.smallestInsets);
	// setLayout(layout);
	//
	// add(id, "w 25!");
	// add(active, "w 20!");
	// add(name, "w 120!");
	// add(value, "w 120!");
	//
	// active.addActionListener(this);
	// name.addActionListener(this);
	// value.addActionListener(this);
	//
	// }

	public void createNew() throws Throwable {

		String xml = streamer.toXML(new Parameter());
		Response response = client.post(getClassURI(),
				new StringRepresentation(xml, MediaType.APPLICATION_XML));
		if (response.getStatus().getCode() != 201)
			throw new RuntimeException("Error creating Parameter with xml;"
					+ xml);
		xml = response.getEntity().getText();
		parameter = (Parameter) streamer.fromXML(xml);
		setEntity(parameter);

	}

	public void setEntity(Object param) {
		parameter = (Parameter) param;
		active.setSelected(parameter.isActive());
		name.setText(parameter.getName());
		value.setText(parameter.getValue());
		id.setText(parameter.getId().toString());
		repaint();
	}

	public void loadEntity(String uri) throws Throwable {
		Response response = client.get(uri);
		if (response.getStatus().getCode() != 200)
			throw new RuntimeException("Error fetching entity " + uri);
		String xml = response.getEntity().getText();
		parameter = (Parameter) streamer.fromXML(xml);
		setEntity(parameter);
	}

	public void refresh() throws Throwable {
		loadEntity(parameter.getUri());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		super.actionPerformed(e);
		
		
		if (!editable)
			return;

		try {
			Object source = e.getSource();
			if (source == active) {
				active.setForeground(Color.RED);
				Response response = client.put(getInstanceURI() + "/"
						+ "active", new StringRepresentation(active
						.isSelected()
						+ ""));
				if (response.getStatus().getCode() != 200)
					throw new RuntimeException(
							"Error changing app active state to "
									+ active.isSelected());
				active.setForeground(Color.BLACK);
			} else if (source == name) {
				EditorUtil
						.saveTextField(client, getInstanceURI(), name, "name");
			} else if (source == value) {
				EditorUtil.saveTextField(client, getInstanceURI(), value,
						"value");
			}
		} catch (Throwable t) {
			t.printStackTrace();
			JOptionPane.showMessageDialog(this, t.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private String getInstanceURI() {
		return getClassURI() + "/" + parameter.getId();
	}

	public void addMouseListener(MouseListener list) {
		super.addMouseListener(list);
		active.addMouseListener(list);
		name.addMouseListener(list);
		value.addMouseListener(list);
	}

	public void removeMouseListener(MouseListener list) {
		super.removeMouseListener(list);
		active.removeMouseListener(list);
		name.removeMouseListener(list);
		value.removeMouseListener(list);
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
		active.setEnabled(editable);
		name.setEditable(editable);
		value.setEditable(editable);
	}

	@Override
	public Object getEntity() {
		return parameter;
	}

}
